package com.xiguthings.waka.web.controller.sysconfig;

import com.fasterxml.jackson.annotation.JsonTypeInfo;
import com.xiguthings.waka.exception.BussinessException;
import com.xiguthings.waka.po.Paper;
import com.xiguthings.waka.po.PaperExample;
import com.xiguthings.waka.po.SysConfig;
import com.xiguthings.waka.po.SysConfigExample;
import com.xiguthings.waka.po.extpo.Page;
import com.xiguthings.waka.publicUtils.StringUtils;
import com.xiguthings.waka.service.sysconfig.ISysConfigService;
import com.xiguthings.waka.web.controller.base.AbstractController;
import com.xiguthings.waka.web.controller.base.ResponseEntity;
import com.xiguthings.waka.web.controller.paper.PaperVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import springfox.documentation.annotations.ApiIgnore;

import javax.annotation.Resource;

@Controller
@RequestMapping("/sysconfig")
@Api(tags = "系统配置")
public class SysConfigController extends AbstractController {
    @Resource
    private ISysConfigService sysConfigService;


    /**
     * 创建查询条件
     *
     * @param sysConfigVo
     * @param example
     * @throws NumberFormatException
     */
    private SysConfigExample.Criteria queryConditions(SysConfigVo sysConfigVo, SysConfigExample example) throws NumberFormatException {

        SysConfigExample.Criteria criteria = example.createCriteria();
        // 如果查询条件为空的
        if (sysConfigVo == null) {
            return criteria;
        }
        // 如果有查询条件
        SysConfig sysConfig = sysConfigVo.getSysConfig();
        // 添加查询条件
        if (sysConfig != null) {
            if (StringUtils.isNotBlank(sysConfig.getConfigId())) {
                criteria.andConfigIdEqualTo(sysConfig.getConfigId());
            }
            if (StringUtils.isNotBlank(sysConfig.getCode())) {
                criteria.andCodeEqualTo(sysConfig.getCode());
            }
            if (StringUtils.isNotBlank(sysConfig.getName())) {
                criteria.andNameLike("%" + sysConfig.getName() + "%");
            }
            if (StringUtils.isNotBlank(sysConfig.getValue())) {
                criteria.andValueLike("%" + sysConfig.getValue() + "%");
            }
            if (StringUtils.isNotBlank(sysConfig.getDescription())) {
                criteria.andDescriptionLike("%" + sysConfig.getDescription() + "%");
            }
        }
        return criteria;
    }

    /**
     * 修改或者添加
     *
     * @param sysConfigVo
     * @return
     * @throws Exception
     */
    @ApiOperation(tags = {"1.控制台"}, value = "修改/添加", httpMethod = "POST",
            notes = "修改时，必须填写“paper.paperId”参数。<br>添加新记录时，growthId、paperCardId、rank是必填的",
            response = ResponseEntity.class)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "sysConfig.configId", value = "主键id", dataType = "String", required = false, paramType = "query"),
            @ApiImplicitParam(name = "sysConfig.code", value = "编码（不可修改）", dataType = "String", required = false, paramType = "query"),
            @ApiImplicitParam(name = "sysConfig.name", value = "名称", dataType = "string", required = false, paramType = "query"),
            @ApiImplicitParam(name = "sysConfig.value", value = "值", dataType = "string", required = false, paramType = "query"),
            @ApiImplicitParam(name = "sysConfig.description", value = "描述", dataType = "string", required = false, paramType = "query")
    })
    @RequestMapping("/save")
    @ResponseBody
    public ResponseEntity save(@ApiIgnore SysConfigVo sysConfigVo) throws Exception {

        // 先判断是不是空参数
        if (sysConfigVo == null || sysConfigVo.getSysConfig() == null) {
            BussinessException.throwThis(CODE_EMPTY_REQUIRED, null);
        }

        SysConfig sysConfig = sysConfigVo.getSysConfig();
        String id = sysConfig.getConfigId();
        //判断是修改还是添加
        String msg = "";
        if (StringUtils.isNotBlank(id)) {
            // 修改
            sysConfigService.updateByPrimaryKeySelective(sysConfig);
            msg = "修改成功";
        } else {
            //添加
            msg = "添加成功";
            sysConfigService.insert(sysConfig);
        }

        return ajaxReturnMap(CODE_SUCCESS, msg, sysConfig.getConfigId());
    }


    @ApiOperation(tags = {"1.控制台", "2.用户端"}, value = "根据主键获取记录详情", httpMethod = "GET", notes = "paperId是必填", response = ResponseEntity.class)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "sysConfig.code", value = "编码（不可修改）", dataType = "String", required = false, paramType = "query"),
            @ApiImplicitParam(name = "sysConfig.configId", value = "主键id", dataType = "String", required = false, paramType = "query")
    })
    @RequestMapping("/detail")
    @ResponseBody
    public ResponseEntity detail(@ApiIgnore SysConfigVo sysConfigVo) throws Exception {
        // 先判断是不是空参数
        if (sysConfigVo == null || sysConfigVo.getSysConfig() == null) {
            BussinessException.throwThis(CODE_EMPTY_REQUIRED, null);
        }
        SysConfig sysConfigDB = null;
        SysConfig sysConfig = sysConfigVo.getSysConfig();
        String id = sysConfig.getConfigId();
        String code = sysConfig.getCode();
        if (StringUtils.isNotBlank(id)) {
            sysConfigDB = sysConfigService.selectByPrimaryKey(id);
        } else if (StringUtils.isNotBlank(code)) {
            sysConfigDB = sysConfigService.selectByCode(code);
        }
        return ajaxReturnMap(CODE_SUCCESS, null, sysConfigDB);
    }


    /**
     * 分页查询
     *
     * @param sysConfigVo
     * @param page
     * @return
     * @throws Exception
     */

    @ApiOperation(tags = {"1.控制台"}, value = "分页查询", httpMethod = "GET", notes = "可根据条件进行筛选", response = ResponseEntity.class)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "sysConfig.configId", value = "主键id", dataType = "String", required = false, paramType = "query"),
            @ApiImplicitParam(name = "sysConfig.code", value = "编码（不可修改）", dataType = "String", required = false, paramType = "query"),
            @ApiImplicitParam(name = "sysConfig.name", value = "名称", dataType = "string", required = false, paramType = "query"),
            @ApiImplicitParam(name = "sysConfig.value", value = "值", dataType = "string", required = false, paramType = "query"),
            @ApiImplicitParam(name = "sysConfig.description", value = "描述", dataType = "string", required = false, paramType = "query"),
            @ApiImplicitParam(name = "page", value = "页码，如果没有值，则使用默认值。开始值从 1 开始计算", dataType = "int", required = false, paramType = "query"),
            @ApiImplicitParam(name = "rows", value = "每页的数量,如果没有值，则使用默认值", dataType = "int", required = false, paramType = "query")})
    @RequestMapping("/pagelist")
    @ResponseBody
    public ResponseEntity pageList(@ApiIgnore SysConfigVo sysConfigVo, @ApiIgnore Page page) throws Exception {
        SysConfigExample example = new SysConfigExample();
        // 创建查询条件
        queryConditions(sysConfigVo, example);
        super.selectByPage(page, example, sysConfigService);
        return ajaxReturnMap(CODE_SUCCESS, null, page);
    }

}
